package com.lanou.fooddemo.util;

import android.util.Log;

import com.google.gson.Gson;
import com.lanou.fooddemo.bean.DataBean;
import com.lanou.fooddemo.bean.JSONBean;
import com.lanou.fooddemo.bean.PicBean;
import com.lanou.fooddemo.bean.SubCardsBean;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 2015/7/27.
 */
public class NetUtil {
    private static final String LOG_TAG = "NetUtil";
    private static final String PATH = "https://api.shiseapp.com/v3/home/?from=2&since=t3WeqMG" +
            "t.Ctv&uuid=31af41e5e906c0b8fae2f6180e47f1c8&_version=3.1.1&_device=Android5.0.1&gps=" +
            "38.883516,121.544622&_channel=wandoujia&_city=";
    public static final String URL_SAME_CITY = "https://api.shiseapp.com/v3/home/?uuid=2d085016747c92a909deba4cb7b9ab8c&_version=3.1.1&_device=Android5.1&gps=38.883426,121.544769&_city=%E5%A4%A7%E8%BF%9E&_channel=yingyongbao";
    public static final String URL_SAME_CITY_NEXT = URL_SAME_CITY + "&from=";
    public static final String URL_BASE = "https://api.shiseapp.com";
    public static final String URL_SAME_CITY_INFORMATION = "uuid=31af41e5e906c0b8fae2f6180e47f1c8&_version=3.1.1&_device=Android5.0.1&gps=38.883474,121.544648&_city=%E5%A4%A7%E8%BF%9E&_channel=wandoujia";
    public static final String URL_RECOMMEND = "https://api.shiseapp.com/v3/home/?type=follow&uuid=31af41e5e906c0b8fae2f6180e47f1c8&_version=3.1.1&_device=Android5.0.1&gps=38.883482,121.544661&_city=%E5%A4%A7%E8%BF%9E&_channel=wandoujia";
    public static final String URL_TOPIC = "https://api.shiseapp.com/v3/discover/?type=feed&uuid=31af41e5e906c0b8fae2f6180e47f1c8&_version=3.1.1&_device=Android5.0.1&gps=38.883482,121.544661&_city=%E5%A4%A7%E8%BF%9E&_channel=wandoujia";
    public static final String URL_RECOMMEND_NEXT = URL_RECOMMEND +"&from=";
    public static final String URL_TOPIC_NEXT = URL_TOPIC + "&from=";
    public static final String URL_RECOMMEND_INFORMATION = "/v3/listing/m/m_flAMGYDsl49lXhA./?uuid=31af41e5e906c0b8fae2f6180e47f1c8&_version=3.1.1&_device=Android5.0.1&gps=38.883486,121.544646&_city=%E5%A4%A7%E8%BF%9E&_channel=wandoujia";
    public static final String URL_SELECTED = "https://api.shiseapp.com/v3/home/?type=jing&uuid=31af41e5e906c0b8fae2f6180e47f1c8&_version=3.1.1&_device=Android5.0.1&gps=38.883455,121.544606&_city=%E5%A4%A7%E8%BF%9E&_channel=wandoujia";
    public static final String URL_SELECTED_NEXT = URL_SELECTED + "&from=";
    public static final String URL_SELECTED_INFORMATION = "/v3/home/?type=jing&uuid=31af41e5e906c0b8fae2f6180e47f1c8&_version=3.1.1&_device=Android5.0.1&gps=38.883455,121.544606&_city=%E5%A4%A7%E8%BF%9E&_channel=wandoujia";
    //话题详情页接口
    //public static final String URL_TOPIC_DETAILS = "https://api.shiseapp.com/v2/event/294/?uuid=31af41e5e906c0b8fae2f6180e47f1c8&_version=3.1.1&_device=Android5.0.1&gps=38.883483,121.544642&_city=%E5%A4%A7%E8%BF%9E&_channel=wandoujia";
   // public static final String URL_TOPIC_DETAILS_NEXT =URL_TOPIC_DETAILS + "&from=";
    public static final String URL_TOPIC_DETAILS = "uuid=31af41e5e906c0b8fae2f6180e47f1c8&_version=3.1.1&_device=Android5.0.1&gps=38.883483,121.544642&_city=%E5%A4%A7%E8%BF%9E&_channel=wandoujia";
    public static final String URL_TOPIC_DETAILS_NEXT =URL_TOPIC_DETAILS + "&from=";


    private static int sameCityPosition = 2;
    private static int recommendPosition = 2;
    private static int topicPosition = 0;
    private static int topicDetailsPosition = -5;

    private static int selectedPosition = 2;
    /**
     * 同城列表增量
     */
    private static final int TOPIC_POSITION_INCREMENT = 20;
    private static final int SAME_CITY_POSITION_INCREMENT = 9;
    private static final int TOPIC_DETAILS_POSITION_INCREMEN = 10;

    //默认大连市
    public static String CITY = "大连";

    public static JSONBean getData(String request) {
        InputStream is = null;
        HttpURLConnection conn = null;
        try {
            //String request = URLEncoder.encode("大连", "utf-8");
            //request = PATH + request;
            request = "https://api.shiseapp.com/v3/home/?uuid=2d085016747c92a909deba4cb7b9ab8c&_version=3.1.1&_device=Android5.1&gps=38.883426,121.544769&_city=%E5%A4%A7%E8%BF%9E&_channel=yingyongbao";
            URL url = new URL(request);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("version", "HTTP/1.1");
            conn.addRequestProperty("User-Agent", "Dalvik/2.1.0 (Linux; U; Android 5.0.1; MX4 Build/LRX22C) Paros/3.2.13");
            conn.addRequestProperty("Host", "api.shiseapp.com");
            conn.setRequestProperty("Connection", "Keep-Alive");
            is = conn.getInputStream();
            byte[] buffer = new byte[1024];
            int length = -1;
            StringBuffer data = new StringBuffer();
            while (-1 != (length = is.read(buffer))) {
                data.append(new String(buffer, 0, length, "utf-8"));
            }
            Log.i(LOG_TAG, "getData() Path = " + request);
            Log.i(LOG_TAG, data.toString());
            Log.i(LOG_TAG, android.os.Build.MODEL);
            if (null != data && 2 < data.length()) {
                Gson gson = new Gson();
                JSONBean jSONBean = gson.fromJson(data.toString(), JSONBean.class);
                if (null != jSONBean.getData() && 0 < jSONBean.getData().size()) {
                    DataBean dataBean = jSONBean.getData().get(0);
                    PicBean picBean = dataBean.getPic();
                    String picUrl = picBean.getUrl();
                    String title = dataBean.getTitle();
                    String desc = dataBean.getDesc();
                    Log.i(LOG_TAG, "title = " + title + "   desc = " + desc);
                    return jSONBean;
                } else {
                    Log.i(LOG_TAG, "data is null");
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取网络数据字符串
     * @param request 网络请求url字符串
     * @return url返回的字符串
     */
    private static String getStringOfNet(String request) {
        InputStream is = null;
        HttpURLConnection conn = null;
        if (null != request) {
            try {
                URL url = new URL(request);
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestProperty("version", "HTTP/1.1");
                conn.addRequestProperty("User-Agent", "Dalvik/2.1.0 (Linux; U; Android 5.0.1; MX4 Build/LRX22C) Paros/3.2.13");
                conn.addRequestProperty("Host", "api.shiseapp.com");
                conn.setRequestProperty("Connection", "Keep-Alive");
                is = conn.getInputStream();
                byte[] buffer = new byte[1024];
                int length = -1;
                StringBuffer data = new StringBuffer();
                while (-1 != (length = is.read(buffer))) {
                    data.append(new String(buffer, 0, length, "utf-8"));
                }
                return data.toString();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                if (null != is) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (null != conn) {
                    conn.disconnect();
                }
            }
        }
        return null;
    }

    /**
     * 从url返回的数据String中解析出JSONBean
     * @param urlDataString url返回的数据String,通过getStringOfNet(String url)获取
     * @return JSONBean
     */
    private static JSONBean getJSONBeanOfString(String urlDataString) {
        if (null != urlDataString) {
            Gson gson = new Gson();
            JSONBean jSONBean = gson.fromJson(urlDataString.toString(), JSONBean.class);
            return jSONBean;
        }
        return null;
    }
    private static JSONBean getJSONBeanOfUrl(String url) {
        String dataString = getStringOfNet(url);
        return getJSONBeanOfString(dataString);
    }

    /**
     * bean 获取方法
     * @param url
     * @return
     */
    public static List<DataBean> getDataBeanForAll(String url) {
        JSONBean jsonBean = getJSONBeanOfUrl(url);
        if (null != jsonBean) {
            List<DataBean> dataBeans = jsonBean.getData();
            if (null != dataBeans && 0 < dataBeans.size()) {
                return dataBeans;
            }
        }
        return null;
    }

    /**
     * 同城数据拉取
     * @param url
     * @return
     */
    public static List<DataBean> getDataBeanListDataForSameCityList(String url) {
        List<DataBean> dataBeans = getDataBeanForAll(url);
        List<DataBean> dataBeansList = new ArrayList<DataBean>();
        if (null != dataBeans && 0 < dataBeans.size()) {
            for (DataBean dataBean : dataBeans) {
                if (DataBean.TYPE_EVENT_CARD.equals(dataBean.getType())) {
                    dataBeansList.add(0, dataBean);
                }
                if (DataBean.TYPE_CONTAINER_CARD.equals(dataBean.getType())) {
                    dataBeansList.add(dataBean);
                }
            }
            return dataBeansList;
        }
        return null;
    }

    /**
     * 同城数据首次拉取接口
     * @return DataBean的List
     */
    public static List<DataBean> getDataBeanListDataForSameCityList() {
        sameCityPosition = 2;
        return getDataBeanListDataForSameCityList(URL_SAME_CITY);
    }
    public static List<DataBean> getDataBeanListDataForRecomment(){
        sameCityPosition = 2;
        return getDataBeanListDataForRecommend(URL_RECOMMEND);
    }
    public static List<DataBean> getDataBeanListDataForSelected(){
        sameCityPosition = 2;
        return getDataBeanListDataForSelected(URL_SELECTED);
    }
    private static List<DataBean> getDataBeanListDataForTopicList(String ulr) {
        return getDataBeanForAll(ulr);
    }


    public static List<DataBean> getDataBeanListDataForTopicList() {
         topicPosition = 0 ;
        return getDataBeanListDataForTopicList(URL_TOPIC);
    }

    public static List<DataBean> getDataBeanListDataForTopicDetailsList(String api) {
        topicDetailsPosition = -5 ;
        api = URL_BASE + api + "?" + URL_TOPIC_DETAILS ;
        return getDataBeanForAll(api);
    }
    public static List<DataBean> getDataBeanListDataForTopicDetailsListForNext(String api){
        topicDetailsPosition += TOPIC_DETAILS_POSITION_INCREMEN ;
        api = URL_BASE + api + "?" + URL_TOPIC_DETAILS + "&from=" + topicDetailsPosition ;
        Log.i("LLL", "api = " + api);
        return getDataBeanForAll(api);
    }







    /**
     * 同城数据非首次拉取接口
     * @return DataBean的List
     */
    public static List<DataBean> getDataBeanListDataForSameCityListForNext() {
        List<DataBean> dataBeanList = getDataBeanListDataForSameCityList(NetUtil.URL_SAME_CITY_NEXT + sameCityPosition);
        sameCityPosition += SAME_CITY_POSITION_INCREMENT;
        return dataBeanList;
    }

    /**
     * 同城详细信息界面数据接口
     * @param api 定位资源的api值
     * @return DataBean的List
     */
    public static List<DataBean> getDataBeanListDataForSameCityInformation(String api) {
        String url = URL_BASE + api + URL_SAME_CITY_INFORMATION;
        Log.i(LOG_TAG, "同城详细接口 " + url);
        return getDataBeanForAll(url);
    }
    public static List<DataBean> getDataBeanListDataForRecommendInformation(String api){
        String url = URL_BASE + api + URL_RECOMMEND_INFORMATION;
        Log.i(LOG_TAG, "推荐详情接口 "+url);
        return getDataBeanForAll(url);
    }
    public static List<DataBean> getDataBeanListDataForSelectedInformation(String api){
        String url = URL_BASE + api + URL_SELECTED_INFORMATION;
        Log.i(LOG_TAG, "精选详情接口 "+url);
        return getDataBeanForAll(url);
    }

    public static List<DataBean> getDataBeanListDataForRecommendTitleForNext(){
        recommendPosition += TOPIC_POSITION_INCREMENT;
        return getDataBeanListDataForRecommend(NetUtil.URL_RECOMMEND_NEXT + recommendPosition);
    }

    public static List<DataBean> getDataBeanListDataForTopicListForNext(){
        topicPosition += TOPIC_POSITION_INCREMENT ;
        return getDataBeanListDataForTopicList(NetUtil.URL_TOPIC_NEXT + topicPosition);
    }
    public static List<DataBean> getDataBeanListDataForSelectedForNext(){
        return getDataBeanListDataForSelected(NetUtil.URL_SELECTED_NEXT + selectedPosition);
    }



    public static List<SubCardsBean> getSubBeanForSameCityTitle(String url) {
        JSONBean jsonBean = getJSONBeanOfUrl(url);
        List<DataBean> dataBeans = jsonBean.getData();
        if (CommonUtil.isListNotNull(dataBeans)) {
            for (DataBean dataBean : dataBeans) {
                List<SubCardsBean> subCardsBeans = dataBean.getSubCards();
                List<SubCardsBean> subCardsBeansReturn = new ArrayList<SubCardsBean>();
                if (CommonUtil.isListNotNull(subCardsBeans)) {
                    for (SubCardsBean subCardsBean : subCardsBeans) {
                        if (SubCardsBean.TYPE_USER_CARD.equals(subCardsBean.getType())) {
                            subCardsBeansReturn.add(subCardsBean);
                        }
                    }
                    return subCardsBeansReturn;
                }
            }
        }
        return null;
    }


    public static List<DataBean> getDataBeanListDataForSelected(String url) {
        JSONBean jsonBean = getJSONBeanOfUrl(url);
        if (null != jsonBean) {
            List<DataBean> dataBeans = jsonBean.getData();
            List<DataBean> dataBeansList = new ArrayList<DataBean>();
            if (null != dataBeans && 0 < dataBeans.size()) {
                for (DataBean dataBean : dataBeans) {
                    if (DataBean.TYPE_CONTAINER_CARD.equals(dataBean.getType())) {
                        dataBeansList.add(dataBean);
                    }
                }
                return dataBeansList;
            }
        }
        return null;
    }

    /**
     * 推荐List接口
     * @param url
     * @return
     */
    public static List<DataBean> getDataBeanListDataForRecommend(String url) {
        JSONBean jsonBean = getJSONBeanOfUrl(url);
        if (null != jsonBean) {
            List<DataBean> dataBeans = jsonBean.getData();
            List<DataBean> dataBeansList = new ArrayList<DataBean>();
            if (null != dataBeans && 0 < dataBeans.size()) {
                for (DataBean dataBean : dataBeans) {
                    if (DataBean.TYPE_CONTAINER_CARD.equals(dataBean.getType())) {
                        dataBeansList.add(dataBean);
                    }
                }
                return dataBeansList;
            }
        }
        return null;
    }
}
